#pragma once
/*Fri, 19 Feb 2010 20:28:49 GMT*/
class CHTTPFileTime:public CTime
{
#define CHECKINT(i,comp,ret) {if(i==comp)return ret;}
public:
	static int GetMonthByName(const CString &name)
	{
		static LPCTSTR const wkday[] =
		{_T("Mon"), _T("Tue"), _T("Wed"), _T("Thu"), _T("Fri"), _T("Sat"), _T("Sun")};
		static LPCTSTR const weekday[] =
		{ _T("Monday"), _T("Tuesday"), _T("Wednesday"), _T("Thursday"),
		_T("Friday"), _T("Saturday"), _T("Sunday") };
		static LPCTSTR const month[]=
		{ _T("Jan"), _T("Feb"), _T("Mar"), _T("Apr"), _T("May"), _T("Jun"),
		_T("Jul"), _T("Aug"), _T("Sep"), _T("Oct"), _T("Nov"), _T("Dec") };

		
		static size_t sizeMonth=sizeof(month)/sizeof(LPCSTR);
		
		for(size_t i=0;i<sizeMonth;i++)
		{
			if(month[i]==name)
			{
				return int(i);
			}
		}
		return -1;
	}
	struct tzinfo {
		CHAR name[5];
		int offset; /* +/- in minutes */
	};

	static int GetLocalOffset(const CString &sLocal)
	{
#define tDAYZONE -60       /* offset for daylight savings time */
		static const struct tzinfo tz[]= {
			{"GMT", 0},              /* Greenwich Mean */
			{"UTC", 0},              /* Universal (Coordinated) */
			{"WET", 0},              /* Western European */
			{"BST", 0 tDAYZONE},     /* British Summer */
			{"WAT", 60},             /* West Africa */
			{"AST", 240},            /* Atlantic Standard */
			{"ADT", 240 tDAYZONE},   /* Atlantic Daylight */
			{"EST", 300},            /* Eastern Standard */
			{"EDT", 300 tDAYZONE},   /* Eastern Daylight */
			{"CST", 360},            /* Central Standard */
			{"CDT", 360 tDAYZONE},   /* Central Daylight */
			{"MST", 420},            /* Mountain Standard */
			{"MDT", 420 tDAYZONE},   /* Mountain Daylight */
			{"PST", 480},            /* Pacific Standard */
			{"PDT", 480 tDAYZONE},   /* Pacific Daylight */
			{"YST", 540},            /* Yukon Standard */
			{"YDT", 540 tDAYZONE},   /* Yukon Daylight */
			{"HST", 600},            /* Hawaii Standard */
			{"HDT", 600 tDAYZONE},   /* Hawaii Daylight */
			{"CAT", 600},            /* Central Alaska */
			{"AHST", 600},           /* Alaska-Hawaii Standard */
			{"NT",  660},            /* Nome */
			{"IDLW", 720},           /* International Date Line West */
			{"CET", -60},            /* Central European */
			{"MET", -60},            /* Middle European */
			{"MEWT", -60},           /* Middle European Winter */
			{"MEST", -60 tDAYZONE},  /* Middle European Summer */
			{"CEST", -60 tDAYZONE},  /* Central European Summer */
			{"MESZ", -60 tDAYZONE},  /* Middle European Summer */
			{"FWT", -60},            /* French Winter */
			{"FST", -60 tDAYZONE},   /* French Summer */
			{"EET", -120},           /* Eastern Europe, USSR Zone 1 */
			{"WAST", -420},          /* West Australian Standard */
			{"WADT", -420 tDAYZONE}, /* West Australian Daylight */
			{"CCT", -480},           /* China Coast, USSR Zone 7 */
			{"JST", -540},           /* Japan Standard, USSR Zone 8 */
			{"EAST", -600},          /* Eastern Australian Standard */
			{"EADT", -600 tDAYZONE}, /* Eastern Australian Daylight */
			{"GST", -600},           /* Guam Standard, USSR Zone 9 */
			{"NZT", -720},           /* New Zealand */
			{"NZST", -720},          /* New Zealand Standard */
			{"NZDT", -720 tDAYZONE}, /* New Zealand Daylight */
			{"IDLE", -720},          /* International Date Line East */
		};
		static size_t sizeTZ=sizeof(tz)/sizeof(tzinfo);
		const CStringA comp(sLocal);
		for(size_t i=0;i<sizeTZ;i++)
		{
			if(tz[i].name==comp)
			{
				return tz[i].offset;
			}
		}
		return -1;
	}
	CHTTPFileTime(const CString& str)
	{
		FromString(str);
	}

	BOOL FromString(const CString& str)
	{
		int iStart=0;
		iStart=str.Find(_T(","));
		CHECKINT(iStart,-1,FALSE);

		iStart++;//19 Feb 2010 20:28:49 GMT
		const CString sDay=str.Tokenize(_T(" "),iStart);		
		CHECKINT(iStart,-1,FALSE);
		const int iDay=_ttoi(sDay);
		CHECKINT(iDay,0,FALSE);

		const CString sMonth=str.Tokenize(_T(" "),iStart);
		CHECKINT(iStart,-1,FALSE);
		const int iMonth=GetMonthByName(sMonth);
		CHECKINT(iMonth,-1,FALSE);

		const CString sYear=str.Tokenize(_T(" "),iStart);
		CHECKINT(iStart,-1,FALSE);
		const int iYear=_ttoi(sYear);
		CHECKINT(iYear,0,FALSE);

		const CString sTime=str.Tokenize(_T(" "),iStart);
		CHECKINT(iStart,-1,FALSE);

			int iTimeStart=0;
			const CString sHour=sTime.Tokenize(_T(":"),iTimeStart);
			CHECKINT(iTimeStart,-1,FALSE);
			const int iHour=_ttoi(sHour);

			const CString sMinute=sTime.Tokenize(_T(":"),iTimeStart);
			CHECKINT(iTimeStart,-1,FALSE);
			const int iMinute=_ttoi(sMinute);

			const CString sSecond=sTime.Tokenize(_T(":"),iTimeStart);
			CHECKINT(iTimeStart,-1,FALSE);
			const int iSecond=_ttoi(sSecond);


		const CString sLocate=str.Tokenize(_T(" "),iStart);
		CHECKINT(iStart,-1,FALSE);
		const int iLocalOffset=GetLocalOffset(sLocate);
		CHECKINT(iLocalOffset,-1,FALSE);
		
		SYSTEMTIME st;
		st.wYear=iYear;
		st.wMonth=iMonth+1;
		st.wDay=iDay;
		st.wHour=iHour;
		st.wMinute=iMinute;
		st.wSecond=iSecond;
		CTime::operator =(CTime(st).GetTime()+iLocalOffset);
		return TRUE;
	}
}; 